package com.ddm.controller;

import com.ddm.service.ICustomerService;
import com.dto.AjaxResult;
import com.entity.Customer;
import com.entity.Employee;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by he on 2025/3/24 20:56
 */
@RestController
@RequestMapping("/admin/customer")
public class CustomerController {
    @Autowired
    public ICustomerService customerService;
    @GetMapping("/{customerId}")
    // 根据customerId获取消费者信息
    public AjaxResult getCustomerById(@PathVariable("customerId") Integer customerId) {
        Customer customer = customerService.selectCustomerById(customerId);
        return customer != null ? AjaxResult.success(customer) : AjaxResult.error("消费者未找到");
    }
    // 根据消费者ID删除
    @DeleteMapping("/{customerId}")
    public AjaxResult deleteCustomerById(@PathVariable Integer customerId) {
        int rows = customerService.deleteCustomerById(customerId);
        return rows > 0 ? AjaxResult.success("删除消费者成功") : AjaxResult.error("删除消费者失败");
    }

    // 分页查询消费者
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam(required = false)  Customer customer) {
        PageHelper.startPage(pageNum, pageSize);
        List<Customer> list = customerService.selectCustomerList(customer);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }
    // 新增消费者
    @PostMapping
    public AjaxResult saveCustomer(@RequestBody Customer customer) {
        customerService.save(customer);
        return AjaxResult.success("新增消费者成功");
    }

    // 修改消费者信息
    @PutMapping("/money")
    public AjaxResult updateMoney(@RequestParam("balance") String balance,
                                   @RequestParam("hairdyeCount") Integer hairdyeCount,
                                   @RequestParam("haircutCount") Integer haircutCount,
                                   @RequestParam("customerId") Integer customerId) {
        Customer customer = customerService.selectCustomerById(customerId);
        customer.setBalance(customer.getBalance().subtract(new BigDecimal(balance)));
        customer.setHairdyeCount(customer.getHairdyeCount() - hairdyeCount);
        customer.setHaircutCount(customer.getHaircutCount() - haircutCount);
        customer.setCustomerId(customerId);
        boolean row = customerService.updateById(customer);
        return row ? AjaxResult.success("修改消费者金额成功") : AjaxResult.error("修改消费者金额失败");
    }
}